package com.gavindon.android.widget

import android.app.Dialog
import android.content.Context
import android.widget.TextView
import com.gavindon.android.R
import com.orhanobut.logger.Logger
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

/**
 * description:
 * Created by liNan on  2022-3-30 11:10
 */
class PayResultDialog : Dialog {
    private var tvContent: TextView? = null

    constructor(context: Context) : super(context, R.style.payDialog) {
        setContentView(R.layout.pay_result_dialog)
        setCanceledOnTouchOutside(false)
        tvContent = findViewById(R.id.tvDialogContent)


    }

    fun showloading() {
        if (!this.isShowing) {
            show()
            tvContent!!.text = "您已支付成功,3秒后可在我的视频中观看"
            rxjavaInterval()
        }

    }

    private var subscribe: Disposable? = null

    private fun rxjavaInterval() {
        val time = 3L
        subscribe = Observable.interval(1, TimeUnit.SECONDS)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({ aLong ->
                if (aLong+1 < time && !subscribe!!.isDisposed) {
                    tvContent!!.text = "您已支付成功，" + (time - aLong - 1) + "秒后可在我的视频中观看..."
                } else {
                    listener?.invoke()
                    dismiss()
                }
            }, { error ->
                Logger.i(error.toString())
            })
    }

    private var listener: (() -> Unit)? = null
    fun setIntervalFinish(listener: () -> Unit) {
        this.listener = listener
    }

    override fun dismiss() {
        subscribe?.dispose()
    }
}